<?php


namespace app\common\business;


use think\Exception;

class SpecsValue extends BusBass
{
    public $model = NULL;

    public function __construct()
    {
        $this->model = new \app\common\model\mysql\SpecsValue();
    }

    //获取规格属性
    public function getBySpecsId($specsId)
    {
        try {
            $result = $this->model->getNormalBySpecsId($specsId, "id,name");
        } catch (\Exception $e) {
            return [];
        }
        $result = $result->toArray();
        return $result;
    }


    //根据ID获取这条记录
    public function getById($id)
    {
        $result = $this->model->find($id);
        if (empty($result)) {
            return [];
        }
        $result = $result->toArray();
        return $result;
    }


    //更新状态的逻辑
    public function status($id, $status)
    {
        //查询 id是否存在
        $res = $this->getById($id);
        if (!$res) {
            throw new Exception('不存在该条记录');
        }
        if ($res['status'] == $status) {
            throw new Exception('状态修改前和修改后一样没有意义');
        }
        $data = [
            "status" => intval($status),
        ];
        try {
            $res = $this->model->updateById($id, $data);
        } catch (\Exception $e) {
            //记录日志
            return false;
        }
        return $res;

    }


    //商品详情页sku数据
    public function dealGoodsSkus($gids, $flagValue)
    {
        //得到键名 第一个规格值
        $specsValueKeys = array_keys($gids);
        foreach ($specsValueKeys as $specsValueKey) {
            $specsValueKey = explode(",", $specsValueKey);
            foreach ($specsValueKey as $k => $v) {
                //$new第一个组是第一个规格 第二个组是第二个规格
                $new [$k][] = $v;
                $specsValueIds[] = $v;
            }

        }
        //去掉重复
        $specsValueIds = array_unique($specsValueIds);

        //获取sku value数据逻辑
        $specsValues = $this->getNormalInIds($specsValueIds);

        //组装数据
        $flagValue = explode(",", $flagValue);
        $result = [];
        foreach ($new as $key => $newValue) {
            $newValue = array_unique($newValue);
            $list = [];
            foreach ($newValue as $vv) {
                $list[] = [
                    "id" => $vv,
                    "name" => $specsValues[$vv]['name'],
                    "flag" => in_array($vv, $flagValue) ? 1 : 0,
                ];
            }
            $result[$key] = [
                "name" => $specsValues[$newValue[0]]['specs_name'],
                "list" => $list,
            ];
        }

        return $result;

    }

    //获取sku value数据逻辑
    public function getNormalInIds($ids)
    {

        if (!$ids) {
            return [];
        }
        try {
            $result = $this->model->getNormalInIds($ids);
        } catch (\Exception $e) {
            return [];
        }
        $result = $result->toArray();
        if (!$result) {
            return [];
        }
        //获取规格数据
        $specsNames = config("specs");
        //组装规格数据
        $specsNamesArrs = array_column($specsNames, "name", "id");

        $res = [];
        foreach ($result as $resultValue) {
            $res[$resultValue['id']] = [
                'name' => $resultValue['name'],
                'specs_name' => $specsNamesArrs[$resultValue['specs_id']] ?? "",
            ];

        }
        return $res;
    }


    //sku数据 组装逻辑
    public function dealSpecsValue($skuIdSpecsValueIds)
    {
        $ids = array_values($skuIdSpecsValueIds);
        $ids = implode(",", $ids);
        //去重 和截取
        $ids = array_unique(explode(',', $ids));

        $result = $this->getNormalInIds($ids);
        if (!$result) {
            return [];
        }

        $res = [];
        foreach ($skuIdSpecsValueIds as $skuId => $specs) {
            $specs = explode(",", $specs);
            //处理SKU默认数据
            $skuStr = [];
            foreach ($specs as $spec) {
                $skuStr[] = $result[$spec]['specs_name'] . ":" . $result[$spec]['name'];
            }
            $res[$skuId] = implode("  ", $skuStr);

        }

        return $res;
    }


}